#!/bin/bash
# Copyright 2014 Google Inc.  All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied.  See the License for
# specific language governing permissions and limitations under the License.

# Deploys the app to App Engine.  For use in the open source environment.
#
# Usage:
#
# tools/push <app_id>
#     Pushes the app to App Engine, after running tests.
#
# tools/push -f <app_id>
#     Fast push without running tests.

pushd "$(dirname $0)" >/dev/null && source common.sh && popd >/dev/null

# Parse command options.
if [ "$1" == "-f" ]; then
  fast_push=yes
  shift
fi
if [ -n "$1" ]; then
  app_id=$1
else
  echo "Usage: tools/push [-f] <app_id>"
  exit 1
fi
if [ -n "$USER_EMAIL" ]; then
  email_option="-e $USER_EMAIL"
fi

# Build.
make dbg || exit 1

# Run tests, unless -f was specified.
if [ "$fast_push" != "yes" ]; then
  echo "Running tests..."
  make test || exit 1
fi

# Push.
if hg id >/dev/null; then
  version=$(hg id -i | sed -e 's/\+$/'$(date -u +-%Y%m%d-%H%M)/)
else
  echo -n "Specify a version name: "
  read version
fi
echo "Pushing to $version.$app_id.appspot.com..."
$APPENGINE_DIR/appcfg.py update $email_option -A $app_id -V $version $APP_DIR
